package com.aeroyun.macross.modular.countryside.controller;

import com.aeroyun.macross.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.aeroyun.macross.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.aeroyun.macross.modular.system.model.Farmer;
import com.aeroyun.macross.modular.countryside.service.IFarmerService;

/**
 * 农民管理控制器
 *
 * @author fengshuonan
 * @Date 2018-08-10 16:19:06
 */
@Controller
@RequestMapping("/farmer")
public class FarmerController extends BaseController {

    private String PREFIX = "/countryside/farmer/";

    @Autowired
    private IFarmerService farmerService;

    /**
     * 跳转到农民管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "farmer.html";
    }

    /**
     * 跳转到添加农民管理
     */
    @RequestMapping("/farmer_add")
    public String farmerAdd() {
        return PREFIX + "farmer_add.html";
    }

    /**
     * 跳转到修改农民管理
     */
    @RequestMapping("/farmer_update/{farmerId}")
    public String farmerUpdate(@PathVariable Integer farmerId, Model model) {
        Farmer farmer = farmerService.selectById(farmerId);
        model.addAttribute("item",farmer);
        LogObjectHolder.me().set(farmer);
        return PREFIX + "farmer_edit.html";
    }

    /**
     * 获取农民管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return farmerService.selectList(null);
    }

    /**
     * 新增农民管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Farmer farmer) {
        farmerService.insert(farmer);
        return SUCCESS_TIP;
    }

    /**
     * 删除农民管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer farmerId) {
        farmerService.deleteById(farmerId);
        return SUCCESS_TIP;
    }

    /**
     * 修改农民管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Farmer farmer) {
        farmerService.updateById(farmer);
        return SUCCESS_TIP;
    }

    /**
     * 农民管理详情
     */
    @RequestMapping(value = "/detail/{farmerId}")
    @ResponseBody
    public Object detail(@PathVariable("farmerId") Integer farmerId) {
        return farmerService.selectById(farmerId);
    }
}
